/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package ui.windows.components.panels.serverInformationPanel;

import exceptions.NoServerException;
import processing.objects.Server;
import utils.tns.GetTNS;

import javax.swing.*;

/**
 * Panel for displaying server information
 *
 * @author rymek
 *         User: rymek
 *         Date: 18.07.11
 *         Time: 15:48
 */
public class ServerInformationPanel {

    // other
    private JPanel serverInformation;
    private JComboBox<String> tnsList;
    private JTextField serverIpAddress;
    private JTextField serverPort;
    private JTextField serverUsername;
    private JPasswordField serverPassword;

    private Object parentFrame;

    /**
     * Default constructor
     */
    public ServerInformationPanel() {

        this.fillTnsDropdown();
        this.setDefaultOperation();
        tnsList.addItemListener(new ServerListItemListener(this));
        tnsList.getEditor().getEditorComponent().addKeyListener(new ServerListKeyAdapter(this));
    }

    /**
     * Constructor used in windows that need this panel
     * strictly as infromation panel only.
     * Disables all fields for edit.
     *
     * @param server server information to display
     */
    public ServerInformationPanel(Server server) {
        this.setInfoMode();
        this.tnsList.addItem(server.getTnsName());
        this.serverIpAddress.setText(server.getUrl());
        this.serverPort.setText(server.getPort());
        this.serverUsername.setText(server.getUserName());
        this.serverPassword.setText(server.getUserPass());
    }

    /**
     * Fills TNS dropdown with available DB environments
     */
    void fillTnsDropdown() {
        this.tnsList.addItem("");
        for (String str : GetTNS.getMapDb().keySet()) {
            this.tnsList.addItem(str);
        }
    }

    /**
     * Generates Server object from user input
     *
     * @return ServerInfo object
     * @throws NoServerException thrown when no server is selected in dropdown
     */
    public Server getServerInfo() throws NoServerException {

        if (this.serverSelected()) throw new NoServerException();

        Server server = GetTNS.getMapDb().get(this.tnsList.getSelectedItem().toString());
        server.setUserName(this.serverUsername.getText());
        server.setUserPass(String.valueOf(this.serverPassword.getPassword()));

        return server;
    }

    /**
     * Sets field flags to display only
     */
    public void setInfoMode() {
        this.tnsList.setEditable(false);
        this.tnsList.setEnabled(false);
        this.serverIpAddress.setEditable(false);
        this.serverIpAddress.setEnabled(false);
        this.serverUsername.setEditable(false);
        this.serverUsername.setEnabled(false);
        this.serverPassword.setEditable(false);
        this.serverPassword.setEnabled(false);
        this.serverPort.setEditable(false);
        this.serverPort.setEnabled(false);
    }

    /**
     * Sets window into default operation - user can input some required models
     */
    void setDefaultOperation() {
        this.serverIpAddress.setEditable(false);
        this.serverPort.setEditable(false);
        this.serverUsername.setEnabled(false);
        this.serverPassword.setEnabled(false);
    }

    /**
     * Checks if any server is selected
     *
     * @return true if server is selected from dropdown
     */
    boolean serverSelected() {
        return tnsList.getSelectedItem().toString().equals("");
    }

    public JPanel getServerInformation() {
        return serverInformation;
    }

    public JComboBox<String> getTnsList() {
        return tnsList;
    }

    public JTextField getServerIpAddress() {
        return serverIpAddress;
    }

    public JTextField getServerPort() {
        return serverPort;
    }

    public JTextField getServerUsername() {
        return serverUsername;
    }

    public JPasswordField getServerPassword() {
        return serverPassword;
    }

    public Object getParentFrame() {
        return parentFrame;
    }

    public void setParentFrame(Object parentFrame) {
        this.parentFrame = parentFrame;
    }
}
